<?php
/* explain:		logs class
 * Project:     marsems
 * File:        Logs.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming	
 * @ author 	Ivan  Ling
 * @ version 3.0
 *
 */
include_once("impl/LogImpl.php");
include_once("domain/LogRequestFeedDomain.php");

class ApiLogImpl extends LogImpl
{
	function __construct($DBTag)
	{
		parent::__construct($DBTag);
	}
	
	public function addRequestFeed($vo)
	{
		if($vo instanceof LogRequestFeedDomain){			
			$sql 	 = "INSERT INTO log_request_feed SET sourceId = ";
			$sql	.= $vo->getSourceId();
			
			$sql 	.= ", startTime = '";
			$sql	.= $vo->getStartTime();
			
			$sql 	.= "', status = ";
			$sql	.= $vo->getStatus();
			
			$id 	= $this->insert($this->DBTag, $sql);
			
			$vo->setId($id);			
		}else{
			$id	= -1;
			
			$this->message = 'insert object is not instance of LogRequestFeedDomain.';
		}
			
		return $id;
	}

	public function setRequestFeedEndTime($vo)
	{
		if($vo instanceof LogRequestFeedDomain){
			$sql  	 = "UPDATE log_request_feed SET endTime = now(), status = ";
			$sql	.= $vo->getStatus();
			
			$sql 	.= " WHERE id = ";
			$sql	.= $vo->getId();
			
			return $this->update($this->DBTag, $sql);			
		}
		
		return false;
	}
	
	public function getLastRequestFeedTime($sourceId)
	{
		$sql = "SELECT endTime FROM log_request_feed WHERE status = 2 AND sourceId = $sourceId ORDER BY endTime DESC LIMIT 1";
				
		$endTime = $this->getColumn($this->DBTag, $sql);
		
		if(empty($endTime)){
			$sql = "SELECT CURTIME()";
				
			$endTime = $this->getColumn($this->DBTag, $sql);
		}
		
		return $endTime;
	}
	
	/* get feed of new add advertiser
	 * 
	 */
	public function getRequestAdvertiser($sourceId)
	{
		$arrback 	= array();
		$conn 		= null;
		$sql  		= "SELECT a.id FROM merchant a, merchant_source b WHERE a.id = b.merchantId AND b.sourceId = $sourceId ";
		$sql  		.= "AND a.id NOT IN (SELECT merchantId FROM log_requested_advertiser WHERE sourceId = $sourceId)";
		
		echo $sql . "\n";
		$arrMerIds  = $this->getColumns($this->DBTag, $sql);
		
		
		$arrAdverIds = array();
		if(count($arrMerIds)){
			$strMerIds  = implode(",", $arrMerIds);
			
			
			$sql  		= "SELECT attributeValue FROM merchant_attributes WHERE attributeId = 5 AND sourceId = $sourceId AND merchantId in ($strMerIds)";
			echo $sql . "\n";
			$arrAdverIds  = $this->getColumns($this->DBTag, $sql);			
		}
		
		return $arrAdverIds;
	}
}
?>
